package org.jetnuke.modules.your_account;

import java.util.Locale;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.util.JetUtil;

public class ChangeSessionLanguage implements IAction {
	String locale;
	String lang;
	String country;

	public void setLocale(String locale) {
		this.locale = locale;
		int k=locale.indexOf("_");
		if(k>-1){
			lang=locale.substring(0,k);
			country=locale.substring(k+1);
		}else
			lang=locale;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.common.IAction#exec(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse,
	 *      org.apache.velocity.VelocityContext, org.jetnuke.common.IModuleInfo,
	 *      java.lang.String, org.jetnuke.common.IUserInfo,
	 *      org.jetnuke.common.IAdminInfo)
	 */
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		Locale lc=null;
		if(lang!=null){
			if(country!=null){
				lc=new Locale(lang,country);
			}
			else
				lc=new Locale(lang);
		}
		if(lc!=null){
			Cookie cookie = new Cookie("LANGUAGE", lc.getLanguage());
			cookie.setMaxAge(-1);
			response.addCookie(cookie);
			
			cookie = new Cookie("COUNTRY", lc.getCountry());
			cookie.setMaxAge(-1);
			response.addCookie(cookie);
		}
		String referer=request.getHeader("Referer"); 
		JetUtil.redirect(vc,response,referer);
		return null;
	}

}
